<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>x-admin</title>
    <link rel="stylesheet" href="/static/xadmin/lib/layui/css/layui.css">
    <style>
    </style>
    <script>
        /^http(s*):\/\//.test(location.href) || alert('请先部署到 localhost 下再访问');
    </script>
</head>
<body>
<div class="layui-form">
    <table class="layui-table">
        <tr>
            <td>
                <label class="layui-form-label">所属角色</label>
            </td>
            <td>
                <div class="layui-input-block">
                    <select name="role_id" id="role_id" lay-verify="required" lay-search>
                        <option value="0">=选择角色=</option>
                        {volist name="role" id="menu"}
                        <option value="{$menu.role_id}">{$menu.role_name}</option>
                        {/volist}
                    </select>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <label class="layui-form-label">管理员名称</label>
            </td>
            <td>
                <div class="layui-input-block">
                    <input type="text" id="names" required  lay-verify="required" placeholder="请输入管理员名称" autocomplete="off" class="layui-input">
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <label class="layui-form-label">密码</label>
            </td>
            <td>
                <div class="layui-input-block">
                    <input type="text" id="password" required  lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <label class="layui-form-label">邮箱</label>
            </td>
            <td>
                <div class="layui-input-block">
                    <input type="text" id="email" required  lay-verify="required" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <label class="layui-form-label">电话</label>
            </td>
            <td>
                <div class="layui-input-block">
                    <input type="text" id="phone" required  lay-verify="required" placeholder="请输入电话" autocomplete="off" class="layui-input">
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <label class="layui-form-label">管理员头像</label>
            </td>
            <td >
                <div class="layui-input-block">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="test1" data-src="">上传图片</button>
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="demo1">
                            <p id="demoText"></p>
                        </div>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <label class="layui-form-label">状态</label>
            </td>
            <td>
                <div class="layui-input-block">
                    <input type="radio" name="status" value="1" title="开启" checked>
                    <input type="radio" name="status" value="2" title="关闭" >
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: center">
                <div class="layui-input-block">
                    <button class="layui-btn" onclick="add_sumbit()">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </td>
        </tr>
    </table>
</div>
<script src="/static/xadmin/lib/layui/jquery-3.2.1.min.js"></script>
<script src="/static/xadmin/lib/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/xadmin/' //静态资源所在路径
    });
    layui.use(['laydate','form'], function(){
        var laydate = layui.laydate;
        var table = layui.table;
        var form = layui.form;
    });
    layui.use('upload', function() {
        var $ = layui.jquery
            , upload = layui.upload;
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: "{:url('admin/upload/upload_img_oss',['type'=>'admin'])}"
            ,accept:'file'  // 允许上传的文件类型
            ,auto:true // 自动上传
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                if(res.code==1){
                    $('#test1').attr('data-src', res.url);  //成功返回路径存到数据库
                    return layer.msg('上传成功',{icon:1});
                }else {
                    return layer.msg('上传失败');
                }
                //上传成功
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
    });


    //添加
    function add_sumbit() {
        //获取值
        var role_id=$("#role_id").val();
        var names=$("#names").val();
        var password=$("#password").val();
        var email=$("#email").val();
        var phone=$("#phone").val();
        var status = $('input[name=status]:checked').val();
        var admin_logo=$("#test1").attr("data-src");
        $.post(
            "{:url('admin/admin/add')}",
            {role_id:role_id,password:password,names:names,email:email,phone:phone,status:status,admin_logo:admin_logo},
            function (data) {
                console.log(data);
                if(data.code==1){
                    layer.msg(data.msg,{icon:1});
                    setTimeout(function () {
                        parent.layer.closeAll();
                        parent.location.reload();
                    },1500);
                }else{
                    layer.msg(data.msg,{icon:2});
                }
            });

    }
</script>
</body>
</html>
